package com.csw.fish;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * @author A
 * @version 1.0
 * @description: TODO
 * @date 2022/11/15 0:30
 */
public class GameWin extends JFrame {

    int width = 1440;
    int height = 900;
    //定义游戏状态 0:未开始 1:游戏中 2:通关失败 3:通关成功 4:暂停
    static int state = 0;
    Bg bg = new Bg();
    //定义空的图片(先把图片画进空的图片再一次加载进主窗口中解决双缓存)
    Image offScreenImage;
    //敌方鱼类
    Enamy enamy;
    Enamy boss;
    //是否生成boss
    Boolean isBoss=false;
    //计数器
    int time = 0;
    //我方鱼对象
    MyFish myFish = new MyFish();

    public void launch() {
        //窗口可见
        this.setVisible(true);
        //设置大小
        this.setSize(width, height);
        //设置居中
        this.setLocationRelativeTo(null);
        //设置窗口大小不可改变
        //this.setResizable(false);
        //设置窗口标题
        this.setTitle("大鱼吃小鱼");
        //设置窗口关闭按钮
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //添加鼠标事件
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                //鼠标左键对应数值为1
                if (e.getButton() == 1 && state == 0) {
                    state = 1;
                    //再次调用paint
                    repaint();
                }
                if (e.getButton()==1&&(state==2||state==3)){
                    reGame();
                    state=1;
                }
            }
        });
        //添加键盘事件
        this.addKeyListener(new KeyAdapter() {

            //按压
            @Override
            public void keyPressed(KeyEvent e) {
                super.keyPressed(e);
                if (e.getKeyCode() == 38) {
                    GameUtils.UP = true;
                }
                if (e.getKeyCode() == 40) {
                    GameUtils.DOWN = true;
                }
                if (e.getKeyCode() == 37) {
                    GameUtils.LEFT = true;
                }
                if (e.getKeyCode() == 39) {
                    GameUtils.RIGHT = true;
                }
                if (e.getKeyCode()==32){
                    switch (state){
                        case 1:
                            state=4;
                            GameUtils.drawWord(getGraphics(), "游戏暂停!", Color.red, 50, 600, 400);
                            break;
                        case 4:
                            state=1;
                            break;
                        default:
                    }
                }
            }

            //抬起
            @Override
            public void keyReleased(KeyEvent e) {
                super.keyReleased(e);
                if (e.getKeyCode() == 38) {
                    GameUtils.UP = false;
                }
                if (e.getKeyCode() == 40) {
                    GameUtils.DOWN = false;
                }
                if (e.getKeyCode() == 37) {
                    GameUtils.LEFT = false;
                }
                if (e.getKeyCode() == 39) {
                    GameUtils.RIGHT = false;
                }
            }

        });

        //每间隔40s调用一次paint
        while (true) {
            repaint();
            time++;
            try {
                Thread.sleep(40);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }

    @Override
    public void paint(Graphics g) {
        //懒加载模式初始化对象
        offScreenImage = createImage(width, height);
        Graphics gImage = offScreenImage.getGraphics();

        //g.drawImage(GameUtils.bgImg, 0, 0, null);
        //定义游戏状态 0:未开始 1:游戏中 2:通关失败 3:通关成功 4:暂停
        bg.paintSelf(gImage, myFish.getLevel());
        switch (state) {
            case 0:
                break;
            case 1:
                myFish.paintSelf(gImage);

                logic();

                for (Enamy enamy : GameUtils.EnamyList) {
                    enamy.panitSelf(gImage);

                }
                if (isBoss){
                    boss.x=boss.x+boss.speed*boss.dir;
                    boss.panitSelf(gImage);
                    if (boss.x<0){
                        //出现boss预警
                        gImage.setColor(Color.RED);
                        gImage.fillRect(boss.x, boss.y, 2400,boss.height/2);
                    }
                }
                break;
            case 2:
                for (Enamy enamy : GameUtils.EnamyList) {
                    enamy.panitSelf(gImage);
                }
                if (isBoss){
                    boss.panitSelf(gImage);
                }
                break;
            case 3:
                myFish.paintSelf(gImage);
                break;
            case 4:
                return;
            default:

        }
        g.drawImage(offScreenImage, 0, 0, null);
    }

    void logic() {
        //关卡难度
        if (GameUtils.score < 5) {
            GameUtils.level = 0;
            myFish.level = 1;
        } else if (GameUtils.score <= 15) {
            GameUtils.level = 1;
        } else if (GameUtils.score <= 50) {
            GameUtils.level = 2;
            myFish.level = 2;
        } else if (GameUtils.score <= 150) {
            GameUtils.level = 3;
            myFish.level = 3;
        } else if (GameUtils.score <= 300) {
            GameUtils.level = 4;
            myFish.level = 3;
        } else if (GameUtils.score > 300) {
            state = 3;
        }
        switch (GameUtils.level) {
            case 4:
                if (time % 60 == 0) {
                    if (Math.random() >= 0) {
                     boss=new Enamy_Boss();
                     isBoss=true;
                    }
                }
            case 3:
            case 2:
                //取余运算.每十次调用一次生成一条鱼
                if (time % 30 == 0) {
                    if (Math.random() >= 0.5) {
                        enamy = new Enamy_3_L();
                    } else {
                        enamy = new Enamy_3_R();
                    }
                    GameUtils.EnamyList.add(enamy);

                }
            case 1:
                //取余运算.每十次调用一次生成一条鱼
                if (time % 20 == 0) {
                    if (Math.random() >= 0.5) {
                        enamy = new Enamy_2_L();
                    } else {
                        enamy = new Enamy_2_R();
                    }
                    GameUtils.EnamyList.add(enamy);

                }

            case 0:
                //取余运算.每十次调用一次生成一条鱼
                if (time % 10 == 0) {
                    if (Math.random() >= 0.5) {
                        enamy = new Enamy_1_L();
                    } else {
                        enamy = new Enamy_1_R();
                    }
                    GameUtils.EnamyList.add(enamy);

                }

                break;
            default:

        }

        for (Enamy enamy : GameUtils.EnamyList) {
            enamy.x = enamy.x + enamy.speed * enamy.dir;
            if (isBoss){
                //boss碰到鱼让鱼消失
                if(boss.getRec().intersects(enamy.getRec())){
                    enamy.x=-200;
                    enamy.y=-200;
                }
                if (boss.getRec().intersects(myFish.getMyRec())){
                    state=2;
                }
            }
            if (myFish.getMyRec().intersects(enamy.getRec())) {
                if (myFish.level >= enamy.type) {
                    System.out.println("吃到鱼了" + GameUtils.score);
                    enamy.x = -200;
                    enamy.y = -200;
                    GameUtils.score=GameUtils.score+enamy.count;
                } else {
                    state = 2;
                }

            }
        }

    }
    //重新开始
    void reGame(){
        GameUtils.EnamyList.clear();
        time=0;
        myFish.level=1;
        GameUtils.score=0;
        myFish.x=700;
        myFish.y=500;
        myFish.width=50;
        myFish.height=50;
        boss=null;
        isBoss=false;

    }
    public static void main(String[] args) {
        new GameWin().launch();
    }
}
